package com.eve.controller.admin;
//
//import com.eve.entity.Picture;
//import com.eve.entity.User;
//import com.eve.service.PictureService;
//import lombok.extern.log4j.Log4j2;
//import org.apache.commons.io.FilenameUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.ui.ModelMap;
//import org.springframework.util.ResourceUtils;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.HttpSession;
//import java.io.File;
//import java.io.IOException;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//import java.util.UUID;
//
//@Controller
////@RequestMapping("/admin")
//@Log4j2
//public class Picture1Controller {
//    @Autowired
//    private PictureService pictureService;
//    /**
//     * 展示所有文件信息
//     */
//    @GetMapping("/showpicture")
//    public String showPicture(HttpSession session, ModelMap modelMap){
//        /**
//         * 在登陆的session中拿到用户信息
//         */
//        User user = (User)session.getAttribute("user");
////        System.out.println(user);
//        //根据用户信息查询所有文件信息
//        List<Picture> PictureList = pictureService.findByUserId(user.getId());
//        //存入作用域
//        modelMap.put("PictureLists",PictureList);
////        System.out.println("---pictkjnfdjk------"+PictureList);
//        return "admin/picture";
//    }
//
//    /**
//     * 上传图片并把信息保存到数据库中‘
//     * (Person person, @RequestParam("file") MultipartFile file
//     */
//    @PostMapping("/upload")
//
//    public  String upload(@RequestParam("pictureaddress") MultipartFile file,
//                          Integer id, String picturename,String picturedescription,
//                          HttpSession session, Model model) throws IOException {
////        System.out.println("----------------------------picture:"+picture);
//        //获取上传文件用户的id
//        User user =(User) session.getAttribute("user");
//        //获取文件原始名称
//        String oldFileName = file.getOriginalFilename();
//        //获取文件的后缀
//        String extension = "."+FilenameUtils.getExtension(file.getOriginalFilename());
//        //生成新的文件名称
//        String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+UUID.randomUUID().toString().replace("-","")+extension;
//        //文件的大小
////        file.getSize();
//        //文件类型
////        file.getContentType();
//        //文件真正路径(绝对路径)
//        String realPath= ResourceUtils.getURL("classpath:").getPath()+"static/upload";
//        System.out.println(realPath);
//        //根据日期生成目录
//        String dateFormat = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//        String dateDirPath =realPath+"/"+dateFormat;
//        File dateDir = new File(dateDirPath);
//        if(!dateDir.exists())dateDir.mkdirs();
//        //处理文件上传
//        File newFile=new File(dateDir,newFileName);
//        file.transferTo(newFile);
//        System.out.println(oldFileName+ newFileName+realPath);
//        //将文件信息放入数据库中
//        Picture picture =new Picture();
//        picture.setPictureaddress("/upload/"+dateFormat+"/"+newFileName)
//                .setUserid(user.getId())
//                .setPicturename(picturename)
//                .setPicturedescription(picturedescription);
//        pictureService.save(picture);
//        model.addAttribute("PictureLists",picture);
//        return "redirect:/admin/showpicture";
//    }
//
//
//}
